<?php 

	//对数据库的操作  增 删 改 查 统计
	
	//数据库相关配置
	define('HOST','localhost');
	define('USER','root');
	define('PASS','');
	define('DBNAME','lamp178');
	define('CHARSET','utf8');

	class Model
	{
		//成员属性
		private $tableName;//表名
		private $link;//链接资源
		private $pk;//主键
		private $fields;//普通字段
		private $where = '';//where条件
		private $field = '';//field条件
		private $limit = '';//limit条件

		//构造方法
		public function __construct($tableName)
		{
			//初始化
			$this->tableName = $tableName;
			// 1.链接数据库
			$this->link = mysqli_connect(HOST,USER,PASS) or die('数据库链接失败');
			//2,选中库
			mysqli_select_db($this->link,DBNAME)or die('选着数据库失败');
			//3,设置字符集
			mysqli_set_charset($this->link,CHARSET);
			//调用方法 获取当前表的字段
			$this->getFileds();
		}

		//获取表中所有字段
		private function getFileds()
		{
			//sql
			$sql = 'desc '.$this->tableName;
			//发送
			$result = mysqli_query($this->link,$sql);
			if($result){
				//声明变量和数据
				$pk = '';//主键
				$arr = [];//普通字段
				//循环取数据
				while($row = mysqli_fetch_assoc($result))
				{
					//判断是否为主键
					if($row['Key'] == 'PRI'){
						$pk = $row['Field'];
					}else{
						$arr[] = $row['Field'];
					}
				}

				//将字段信息存到成员属性中
				$this->pk = $pk;
				$this->fields = $arr;
			}else{
				return false;
			}
		}
		//添加方法
		/*
			add()
			insert into $this->tableName(username,password) values(zhi1,zhi2);
		*/
		public function add(array $arr)
		{
			//检测是否存在非法字段
			foreach ($arr as $k => $v) {
				if(!in_array($k,$this->fields)){
					die('您传递的数据,存在非法字段,请检测:'.$k);
				}
			}
			//获取字段
			$keys = array_keys($arr);
			$k_sql = implode(',',$keys);
			
			//获取值
			$value = array_values($arr);
			$v_sql = '"'.implode('","',$value).'"';

			//准备sql
			$sql = 'insert into '.$this->tableName.'('.$k_sql.') values('.$v_sql.')';
			//执行sql
			$result = mysqli_query($this->link,$sql);
			if($result){
				//返回刚插入的数据id
				return mysqli_insert_id($this->link);
			}else{
				return false;
			}
		}
		//删除操作
		// del($id)
		// delete from user where id = $id;
		public function del($id)
		{
			//sql
			$sql = 'delete from '.$this->tableName.' where '.$this->pk.' = '.$id;
			//执行
			$result = mysqli_query($this->link,$sql);
			if($result){
				//返回受影响行数
				return mysqli_affected_rows($this->link);
			}else{
				return false;
			}
		}

		// 修改方法 update
		//['username'=>'wangwu','password'=>123456,'id'=>1]
		// update user set字段=值,字段=值, where id = ?
		public function update(array $arr)
		{
			$ksql = '';
			$where = '';
			foreach ($arr as $k => $v) {
				if($k == $this->pk){
					$where = ' where '.$k.' = '.$v;
				}else{
					$ksql .= $k.'="'.$v.'",';
				}
			}
			if($where == '')
			{
				die('缺少where条件');
			}

			$ksql = rtrim($ksql,',');

			$sql = 'update '.$this->tableName.' set '.$ksql.$where;
			//执行
			$result = mysqli_query($this->link,$sql);
			if($result){
				//返回受影响行数
				return mysqli_affected_rows($this->link);
			}else{
				return false;
			}
		}

		//find($id) 单条查询
		// select * from user where  id = ?
		public function find($id)
		{
			$sql = 'select * from '.$this->tableName.' where  '.$this->pk.' = '.$id;
			//执行
			$result = mysqli_query($this->link,$sql);
			//判断
			if($result){
				return mysqli_fetch_assoc($result);
			}else{
				return false;
			}
		}

		//select()
		// select * from user;
		public function select()
		{

			//检测是否存在where条件
			$w = '';
			if($this->where)
			{
				$w = ' where '.$this->where;
			}

			//检测是否存在字段条件
			$f = '*';
			if($this->field){
				$f = $this->field;
			}

			//检测是否有limit条件
			$l = '';
			if($this->limit)
			{
				$l = ' limit '.$this->limit;
			}

			//清空所有的条件
			$this->limit = '';
			$this->where = '';
			$this->field = '';

			//拼接sql
			$sql = 'select '.$f.' from '.$this->tableName.$w.$l;
			// echo $sql;
			
			//执行
			$result = mysqli_query($this->link,$sql);
			//判断
			if($result){
				$res = [];//先定义
				//循环提取数据
				while($row = mysqli_fetch_assoc($result))
				{
					$res[] = $row;
				}
				return $res;
			}else{
				return false;
			}
		}

		//where 条件方法
		public function where($w)
		{
			$this->where = $w;
			return $this;
		}

		//field 条件方法
		public function field($f)
		{
			$this->field = $f;
			return $this;
		}

		//limit 
		public function limit($l)
		{
			$this->limit = $l;
			return $this;
		}

		//统计方法
		public function count()
		{
			// 检测是否有where条件
			$w = '';
			if($this->where){
				$w = ' where '.$this->where;
			}
			//sql
			$sql = 'select count(*) as total from '.$this->tableName.$w;

			//执行
			$result = mysqli_query($this->link,$sql);
			//处理
			if($result){
				return mysqli_fetch_assoc($result)['total'];
			}else{
				return false;
			}

		}

		//析构方法
		public function __destruct()
		{
			mysqli_close($this->link);
		}
	}


	//实例化对象
	// $user = new Model('user');

	// $arr = ['username'=>'zhangsan','password'=>'zhangsan123','type'=>'zhangsan@qq.com'];

	// $res = $user->add($arr);
	// var_dump($res);

	// $num = $user->del(5);
	// var_dump($num);

	// $arr = ['username'=>'zhangsan123','password'=>'zhangsan123','id'=>13];

	// $num = $user->update($arr);
	// var_dump($num);

	// $res = $user->find(13);

	// $res = $user->select();
	//select * from user;

	// $res = $user->where('id < 100')->select();
	//select * from user where id < 100;

	// $res = $user->field('id,username')->where('id<100')->select();
	//select id,username form user where id < 100;
	// $res = $user->where('id<100')->field('id,username')->limit('10,10')->select();

	// var_dump($res);

	// $res = $user->select();
	// var_dump($res);


	// $res = $user->where('id < 100')->count();
	// var_dump($res);
 ?>